<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <!-- 引入样式 -->
  <link rel="stylesheet" href="../../plugins/element-ui/index.css" />
  <link rel="stylesheet" href="../../styles/common.css" />
  <link rel="stylesheet" href="../../styles/page.css" />
  <style>
    .selectInput {
      position: relative;
      width: 100%;
      min-width: 100px;
    }
    .selectInput .flavorSelect {
      position: absolute;
      width: 100%;
      padding: 0 10px;
      border-radius: 3px;
      border: solid 1px #FF903D;
      line-height: 30px;
      text-align: center;
      background: #fff;
      top: 50px;
      z-index: 99;
    }
    .selectInput .flavorSelect .items {
      cursor: pointer;
      display: inline-block;
      width: 100%;
      line-height: 35px;
      border-bottom: solid 1px #f4f4f4;
      color: #666;
    }
    .selectInput .flavorSelect .none {
      font-size: 14px;
    }

    #food-add-app .uploadImg .el-form-item__label::before{
          content: '*';
          color: #F56C6C;
          margin-right: 4px;
      }

  </style>
</head>
<body>
   <div class="addBrand-container" id="food-add-app">
    <div class="container">
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        :rules="rules"
        :inline="true"
        label-width="180px"
        class="demo-ruleForm"
      >
        <div>
          <el-form-item
            label="职位名称:"
            prop="name"
          >
            <el-select
                  v-model="ruleForm.name"
                  placeholder="请选择职位"
            >
              <el-option v-for="(item,index) in zhiweiList" :key="index" :label="item" :value="item"/>
            </el-select>
          </el-form-item>

          <el-form-item
                  label="所属部门:"
                  prop="did"
          >
            <el-select
                    v-model="ruleForm.did"
                    placeholder="请选择部门"
            >
              <el-option v-for="(item,index) in deptList" :key="index" :label="item.name" :value="item.id"/>
            </el-select>
          </el-form-item>

          <el-form-item
                  label="职位级别:"
                  prop="code"
          >
            <el-input
                    v-model="ruleForm.code"
                    placeholder="自动获取职位级别"
                    :disabled="true"
            />
          </el-form-item>
        </div>

        <div class="subBox address">
          <el-form-item>
            <el-button @click="goBack()">
              取消
            </el-button>
            <el-button
              type="primary"
              @click="submitForm('ruleForm')"
            >
              保存
            </el-button>
            <el-button
              v-if="actionType == 'add'"
              type="primary"
              class="continue"
              @click="submitForm('ruleForm','goAnd')"
            >
              保存并继续添加职位
            </el-button>
          </el-form-item>
        </div>
      </el-form>
    </div>
  </div>
    <!-- 开发环境版本，包含了有帮助的命令行警告 -->
    <script src="../../plugins/vue/vue.js"></script>
    <!-- 引入组件库 -->
    <script src="../../plugins/element-ui/index.js"></script>
    <!-- 引入axios -->
    <script src="../../plugins/axios/axios.min.js"></script>
    <script src="../../js/request.js"></script>
    <script src="../../js/validate.js"></script>
    <script src="../../js/index.js"></script>
    <script src="../../api/posts.js"></script>
    <script>
      new Vue({
        el: '#food-add-app',
        data() {
          return {
            actionType: '',
            id: '',
            code:1,
            inputStyle  : {'flex':1},
            num:1,
            deptList:[],
            ruleForm  : {},
            mak: false,
            session:0,
            zhiweiList:["董事长","总经理","副经理","职员"]
          }
        },
        computed: {
          rules () {
            return {
              'name': [
                {'required': true, 'message': '请选择职位名称', 'trigger': 'blur'}
              ],
              'did': [
                {'required': true, 'message': '请选择部门名称', 'trigger': 'blur'}
              ],
            }
          }
        },
        created() {
          this.session=window.sessionStorage.getItem("session");
          if (this.session!=0){
            this.zhiweiList=["总经理","副经理","职员"]
          }
          this.getDeptList()
          this.id = requestUrlParam('id')
          this.actionType = this.id ? 'edit' : 'add'
          if (this.id) {
            this.init()
          }
        },
        mounted() {
        },
        methods: {
          // 获取部门信息
          getDeptList () {
            getdeptList().then(res => {
              if (res.code === 1) {
                this.deptList = res.data
              } else {
                this.$message.error(res.msg || '操作失败')
              }
            })
          },
          async init () {
            findById(this.id).then(res => {
              console.log(res)
              if (String(res.code) === '1') {
                this.ruleForm = { ...res.data }
              }else {
                this.$message.error(res.msg || '操作失败')
              }
            })
          },

          submitForm(formName, st) {
            this.$refs[formName].validate((valid) => {
              if (valid) {
                let params = {...this.ruleForm}
                if (this.actionType == 'add') {
                  addPosts(params).then(res => {
                    if (res.code === 1) {
                      this.$message.success('职位添加成功！')
                      if (!st) {
                        this.goBack()
                      }
                    } else {
                      this.$message.error(res.msg || '操作失败')
                    }
                  }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                  })
                } else {
                  editPosts(params).then(res => {
                    if (res.code === 1) {
                      this.$message.success('职位修改成功！')
                      this.goBack()
                    } else {
                      this.$message.error(res.msg || '操作失败')
                    }
                  }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                  })
                }
              } else {
                return false
              }
            })
          },
          
          goBack(){
            window.parent.menuHandle({
              id: '4',
              url: '/backend/page/posts/list.html',
              name: '职位管理'
            },false)
          }
        },
        watch: {
          'ruleForm.name': function (val) {
            if (this.session==0){
              if (this.actionType=="add"){
                if (val=="董事长"){
                  this.ruleForm.code=1;
                }
                if (val=="总经理"){
                  this.ruleForm.code=2;
                }
                if (val=="副经理"){
                  this.ruleForm.code=3;
                }
                if (val=="职员"){
                  this.ruleForm.code=4;
                }
              }else {
                this.ruleForm.code=this.ruleForm.code+1;
              }
            }else {
              if (val=="总经理"){
                this.ruleForm.code=1;
              }
              if (val=="副经理"){
                this.ruleForm.code=2;
              }
              if (val=="职员"){
                this.ruleForm.code=3;
              }
            }
          }
        }
      })
    </script>
</body>
</html>